import { Assets, ImageSpec } from "../consts/Assets";
import RootAutomatorWrapper from "../core/RootAutomatorWrapper";
import ScreenManager from "../core/ScreenManager";
import Utils from "../Utils";

export default class MoleAction extends bt.Action {
    public static create(): MoleAction {
        return new MoleAction();
    }

    constructor(private moles?: ImageSpec[]) {
        super();
        this.moles = [
            Assets.images.img_skip,
            Assets.images.img_skip2,
            Assets.images.btn_quick_use,
        ]
    }

    execute(input: bt.BlackBoard): number {
        return this.moles.some(el => {
            if (ScreenManager.matchImage(el)) {
                if (el.pt) {
                    RootAutomatorWrapper.click(el.pt);
                } else if (el.findbtn) {
                    let clicked = el.findbtn.points.some(pt => {
                        if (images.detectsColor(ScreenManager.screenshot, el.findbtn.color, pt[0], pt[1], 20)) {
                            return RootAutomatorWrapper.click(pt);
                        }
                    });

                    if (!clicked) {
                        RootAutomatorWrapper.click(el.findbtn.points[0]);
                    }
                } else {
                    RootAutomatorWrapper.click(Utils.getCenter(el.region));
                }
                return true;
            }
        }) ? bt.State.success : bt.State.failure;
    }
}